import os
from time import sleep

import allure

import page
from page.page_glsx import PageGlsx
from page.page_login import PageLogin

from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]

@allure.epic("跟老师学页面")
class TestGlsxAd:
    num = 0
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.glsxad = PageGlsx(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        try:
            cls.login.pagelogin_close_login()
        except:
            pass
        cls.login.pagelogin_close_first_page_tanchuang()

    @classmethod
    def teardown_class(cls):
        cls.num = 0
        GetDriver.quit_app_driver()

    @classmethod
    def setup_method(cls):
        cls.login.pagelogin_click_mine()
        username = os.environ['GLSX_AD_USERNAME_AUTO'] + str(cls.num)
        # username ='1110000217' + str(cls.num)
        cls.login.pagelogin(username, password='123456')
        if cls.num == 9:
            cls.num = 0
        else:
            cls.num += 1

    @classmethod
    def teardown_method(cls):
        cls.login.pagelogin_click_mine()
        cls.login.pagelogin_logout()

    @allure.story("跟老师学弹窗测试用例")
    def test01_glsxad_tanchuang(self):
        self.glsxad.pageglsx_click_glsx_tab()
        self.glsxad.pageglsx_click_tanchuang()
        class_type = self.glsxad.pageglsx_get_class_type()
        try:
            sleep(3)
            assert self.glsxad.pageglsx_logic_selected(class_type)
            log.info('断言成功')
            self.glsxad.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxad.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxad.pageglsx_return_to_glsxpage_keycode()

    @allure.story("跟老师学banner测试用例")
    def test02_glsxad_banner(self):
        self.glsxad.pageglsx_click_glsx_tab()
        try:
            self.glsxad.pageglsx_click_close_tanchuang()
        except:
            pass
        self.glsxad.pageglsx_click_banner()
        class_type = self.glsxad.pageglsx_get_class_type()
        try:
            sleep(3)
            assert self.glsxad.pageglsx_logic_selected(class_type)
            log.info('断言成功')
            self.glsxad.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxad.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxad.pageglsx_return_to_glsxpage_keycode()